package utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author jiangfeng on 2023/6/6
 */
public class ArrayUtil {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(toArray(1234, 35, 2)));
        System.out.println(Arrays.deepToString(toDArray("[[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]]")));
        System.out.println(Arrays.deepToString(toDArray("[3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2]")));
        System.out.println(Arrays.deepToString(toDArray("3,1,2,2],[1,4,4,5],[2,4,2,2],[2,4,2,2")));
        System.out.println(Arrays.deepToString(toDArray("3,1,2,2,3],[1,490,4,5],[2,4,2,55,2],[2,4,2,2")));
    }

    public static int[] toArray(int... values) {
        return values;
    }

    public static int[][] toDArray(String value) {
        if (value.startsWith("[[")) {
            value = value.substring(2, value.length());
        } else if (value.startsWith("[")) {
            value = value.substring(1, value.length());
        }

        if (value.endsWith("]]")) {
            value = value.substring(0, value.length() - 2);
        } else if (value.endsWith("]")) {
            value = value.substring(0, value.length() - 1);
        }

        // 开始计算
        List<int[]> list = new ArrayList();
        char[] charArray = value.toCharArray();
        List<Integer> l = new ArrayList();
        for (int i = 0; i < charArray.length; i++) {
            char c = charArray[i];
            if (c == '[') {
                l = new ArrayList();
            } else if (Character.isDigit(c)) {
                // 需要识别所有数字
                StringBuilder sb = new StringBuilder();
                sb.append(c);
                while (i < charArray.length - 1 && Character.isDigit(charArray[i + 1])) {
                    sb.append(charArray[i + 1]);
                    i++;
                }
                l.add(Integer.valueOf(sb.toString()));
            } else if (c == ']') {
                list.add(l.stream().mapToInt(Integer::valueOf).toArray());
            }
        }
        // 把最后一个加上
        list.add(l.stream().mapToInt(Integer::valueOf).toArray());
        return list.toArray(new int[0][0]);
    }
}
